package com.project.myproject;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Honorable(mabingqian)
 * @description
 * @date 2020/6/30 18:29
 * @update （更新、优化，注明点）
 */
@RestController
@RequestMapping("/ma/test")
public class TestController {

    @Resource
    private StringEncryptor encryptor;

    private static int i = 0;

    /**
     * get 请求
     * @param request
     * @param name
     * @param reqInfoBO
     * @return
     */
    @RequestMapping(value = "/get1", method = RequestMethod.GET)
    public RspInfoBO getMethod1(HttpServletRequest request, String name, @RequestBody TestReqBO reqInfoBO) {
        RspInfoBO result = new RspInfoBO();
        System.out.println(request.toString());
        System.out.println("name : " + name);
        result.setCode("200");
        result.setMsg("ok");

        SysUserInfo sysUserInfo = reqInfoBO.getSysUserInfo();
        Long saasCompanyId = reqInfoBO.getSysUserInfo().getCompanyId();
        Long saasUserId = reqInfoBO.getSysUserInfo().getSaasUserId();
        String appKey = reqInfoBO.getSysUserInfo().getAppKey();
        String accessToken = reqInfoBO.getAccessToken();
        return result;
    }

    @RequestMapping(value = "/get2", method = RequestMethod.GET)
    public String getMethod2(HttpServletRequest request, @RequestParam("name") String name) {
        System.out.println(request.toString());
        System.out.println("name : " + name);
        return "ok";
    }

    @RequestMapping(value = "/get3", method = RequestMethod.GET)
    public RspInfoBO getMethod3(HttpServletRequest request, String name, @RequestHeader("EZO-UserId") String userId) {
        RspInfoBO result = new RspInfoBO();
        System.out.println("name : " + name);
        System.out.println("userId : " + userId );
        i++;
        System.out.println("========== 请求 I : " + i);
        if (i % 2 == 0) {
            throw new RuntimeException("error");
        } else {
            result.setCode("200");
            result.setMsg("ok");
            return result;
        }
    }


    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public RspInfoBO postTest(@RequestBody TestReqBO reqInfoBO) {
        SysUserInfo sysUserInfo = reqInfoBO.getSysUserInfo();
        Long saasCompanyId = reqInfoBO.getSysUserInfo().getCompanyId();
        Long saasUserId = reqInfoBO.getSysUserInfo().getSaasUserId();
        String appKey = reqInfoBO.getSysUserInfo().getAppKey();
        String accessToken = reqInfoBO.getAccessToken();

        RspInfoBO result = new RspInfoBO();
        result.setCode("200");
        result.setMsg("ok");
        return result;
    }

    @RequestMapping(value = "/sent-mail", method = RequestMethod.POST)
    public RspInfoBO sentMail(@RequestParam(value = "file") MultipartFile[] file, Map<String, String> files, HttpServletRequest request) {
        RspInfoBO result = new RspInfoBO();
        result.setCode("200");
        result.setMsg("ok");

        for (MultipartFile multipartFile : file) {
            String name = multipartFile.getName();
        }
        return result;
    }

    @RequestMapping(value = "/redirect", method = RequestMethod.POST)
    public void redirectTest(HttpServletRequest request, HttpServletResponse response, @RequestBody TestReqBO reqInfoBO) throws IOException {
        StringBuffer requestURL = request.getRequestURL();
        System.out.println(requestURL.toString());

        response.sendRedirect("https://www.baidu.com");
    }

    @RequestMapping(value = "/get4", method = RequestMethod.GET)
    public RspInfoBO getRe(HttpServletRequest request, String name, @RequestBody(required = false) TestReqBO reqInfoBO) {
        RspInfoBO result = new RspInfoBO();
        System.out.println(request.toString());
        System.out.println("name : " + name);
        result.setCode("200");
        result.setMsg("ok");
        return result;
    }

    @RequestMapping(value = "/del1", method = RequestMethod.DELETE)
    public RspInfoBO getDel(HttpServletRequest request, String name, @RequestBody(required = false) TestReqBO reqInfoBO) {
        RspInfoBO result = new RspInfoBO();
        System.out.println(request.toString());
        System.out.println("name : " + name);
        result.setCode("200");
        result.setMsg("ok");
        return result;
    }

    @RequestMapping(value = "put1", method = RequestMethod.PUT)
    public RspInfoBO getPut(HttpServletRequest request, String name, @RequestBody(required = false) TestReqBO reqInfoBO) {
        RspInfoBO result = new RspInfoBO();
        System.out.println(request.toString());
        System.out.println("name : " + name);
        result.setCode("200");
        result.setMsg("ok");
        return result;
    }


    /**
     * 测试jasypt加密解密
     */
    @GetMapping("/jasypt")
    public RspObjectBO<String> testJasypt(String content) {
        RspObjectBO<String> result = new RspObjectBO<>();
        String encryptPwd = encryptor.encrypt(content);
        System.out.println("加密: " + encryptPwd);
        System.out.println("解密：" + encryptor.decrypt(encryptPwd));

        result.setCode("200");
        result.setMsg("succ");
        result.setData(encryptPwd);
        return result;
    }
}
